import {
  wxRequest
} from '../../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    pageNo: 1,
    hasNextPage: false,
    list: [],
    keyword: '',
    screenTabId: '',
    orgTypeName: '代理商',
    orgType: '',
    orgName: '',
    address: [],
    pageData: {},
    orgList: [
      { id: 4, value: '供应商' },
      { id: 2, value: '代理商' },
      { id: 3, value: '体验店' },
      { id: 5, value: '区域经理' }
    ],
  },

  onLoad: function(options) {
    if (options.orgType) {
      this.setData({
        orgType: options.orgType
      })
    } else {
      this.setData({
        orgType: wx.getStorageSync('userInfo').orgType
      })
    }
    let orgType = wx.getStorageSync('userInfo').orgType
    if (orgType == 2) {
      this.orgList = [
        { id: 2, value: '代理商' },
        { id: 3, value: '体验店' },
      ]
    } else if (orgType == 3) {
      this.orgList = [
        { id: 3, value: '体验店' },
      ]
    } else if (orgType == 4) {
      this.orgList = [
        { id: 4, value: '供应商' },
        { id: 2, value: '代理商' },
        { id: 3, value: '体验店' },
        { id: 5, value: '区域经理' }
      ]
    } else if (orgType == 5) {
      [
        { id: 2, value: '代理商' },
        { id: 3, value: '体验店' },
        { id: 5, value: '区域经理' }
      ]
    }
    this.setData({ orgList: this.orgList })
  },

  onShow: function() {
    this.getOrgType();
    this.findBizOrg();
    setTimeout(() => {
      this.getlist();
    }, 500)
  },

  //查询列表
  getlist: function() {
    let currUserInfo = wx.getStorageSync("userInfo");
    let params = {
      keyword: this.data.keyword,
      orgName: this.data.orgName,
      pageNo: this.data.pageNo,
      disId: this.data.address.length > 2 ? this.data.address[2].code : '',
      cityId: this.data.address.length > 1 ? this.data.address[1].code : '',
      proId: this.data.address.length > 0 ? this.data.address[0].code : '',
      orgType: this.data.orgType==1?null:this.data.orgType,
      userType: 2
    };

    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/sys/protected/user/findEmployee',
      method: 'POST',
      data: params
    }, (res) => {
      let list = res.list;
      if (this.data.pageNo !== 1) {
        list = this.data.list.concat(list);
      }

      this.setData({
        list: list,
        pageData: res.page,
        pageNo: res.page.pageNo,
        hasNextPage: res.page.hasNextPage,
      });

    })
  },



  //下拉刷新
  onPullDownRefresh: function() {
    this.refreshData();
    wx.stopPullDownRefresh()
  },

  refreshData: function() {
    this.setData({
      pageNo: 1
    })
    this.getlist();
  },

  //触底时，加载更多
  onReachBottom: function() {
    if (this.data.hasNextPage) {
      this.setData({
        pageNo: this.data.pageNo + 1
      });
      this.getlist();
    }
  },

  //搜索关键词
  seachInputConfirm: function(e) {
    this.setData({
      pageNo: 1,
      keyword: e.detail.value
    })
    this.getlist();
  },
  
  seachBtn: function () {
    this.setData({
      pageNo: 1,
      keyword: this.data.keyword
    })
    this.getlist();
  },

  seachInputVal: function(e) {
    var inputval = e.detail.value;
    console.log(e)
    this.setData({
      keyword: inputval
    })
  },

  //清空
  clearInput: function() {
    this.setData({
      keyword: ''
    })
    this.getlist();
  },



  //********************筛选更多********************
  popupRight: function() {
    this.setData({
      showPopup: true
    })
  },

  popupCancel: function() {
    this.setData({
      showPopup: false
    })
  },
  //重置
  popupReset: function() {
    this.setData({
      orgType: wx.getStorageSync('userInfo').orgType,
      orgName: '',
      pageNo: 1,
      disId: '',
      address: [],
      quyu: false
    })
  },


  popupConfirm: function() {
    this.setData({
      pageNo: 1
    })
    this.getlist();
    this.popupCancel();
  },

  //选择状态
  checkStatus: function(e) {
    var orgType = e.currentTarget.dataset.id
    this.setData({
      orgType
    })
  },

  orgNameInput: function(e) {
    var orgName = e.detail.value;
    console.log(orgName, 'orgName')
    this.setData({
      orgName
    })
  },

  clearorgNameInput: function() {
    this.setData({
      orgName: ''
    })
  },


  chooseAddress: function() {
    this.setData({
      quyu: true
    })
  },
  addresscancel: function() {
    this.setData({
      quyu: false
    })
  },

  addressDetail: function(e) {
    this.setData({
      address: e.detail,
      quyu: false
    })
  },

  //********************筛选更多end********************





  //选择筛选项
  chooseItem: function(e) {
    let _this = this;
    let screenTabId = _this.data.screenTabId;
    let itemId = e.currentTarget.dataset.id;
    let itemName = e.currentTarget.dataset.name;
    if (screenTabId == "0") {
      _this.setData({
        orgType: itemId,
        orgTypeName: itemName,
        bizOrg: '',
        bizOrgName: '商业组织'
      });
      _this.findBizOrg(); //查询商业组织
    } else if (screenTabId == "1") {
      _this.setData({
        bizOrg: itemId,
        bizOrgName: itemName
      });
    }

    _this.cancel();
    this.setData({
      pageNo: 1
    });
    this.getlist();
  },




  //取消选择
  cancel: function() {
    this.setData({
      screenTabId: ""
    });
  },

  // 组织类型查询
  getOrgType: function() {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/org/protected/bizOrg/findOrgType',
      method: 'post',
      data: {}
    }, (res) => {
      var list = res.list
      console.log(list,1111);
      this.setData({
        orgTypeList: list,
      })
    })
  },


  // 商业组织
  findBizOrg: function() {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/org/protected/bizOrg/findBizOrg',
      method: 'POST',
      data: {
        orgType: wx.getStorageSync('userInfo').orgType
      }
    }, (res) => {
      console.log(res.list, 'bizOrgList')
      this.setData({
        bizOrgList: res.list
      })
    })
  },


  //编辑员工数据
  handleEdit: function(e) {
    var item = e.currentTarget.dataset.item
    console.log(item);

    wx.setStorageSync('staffItem', item)
    wx.navigateTo({
      url: '/pages/publish/staff/index',
    })
  },

  handleDetail: function(e) {
    var item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '/pages/data/index?name=' + item.realName,
    })
  }, 

  //删除员工数据
  handlDelet: function(e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '提示',
      confirmText: '确定',
      cancelText: '取消',
      content: '确定删除该员工',
      success: function(res) {
        if (res.confirm) {
          wx.showToast({
            icon: 'none',
            title: '删除成功',
          })
          that.delectRequest(id)
        }
      }

    })
  },

  delectRequest: function(id) {
    var that = this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + "/sys/protected/user/deleteById?id=" + id,
      method: 'POST',
    }, (res) => {
      console.log(res)
      wx.showToast({
        title: "删除成功",
      })
      that.getlist()
    })
  }

})